package com.xingshentech.ugv.operate.module.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.xingshentech.ugv.operate.base.ApplicationShare;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

    public static boolean isIP(String addr) {
        if (addr.length() < 7 || addr.length() > 15 || "".equals(addr)) {
            return false;
        }
        /**
         * 判断IP格式和范围
         */
        String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
        Pattern pat = Pattern.compile(rexp);
        Matcher mat = pat.matcher(addr);
        boolean ipAddress = mat.find();

        // 对之前的ip判断的bug在进行判断
        if (ipAddress == true) {
            String ips[] = addr.split("\\.");
            if (ips.length == 4) {
                try {
                    for (String ip : ips) {
                        if (Integer.parseInt(ip) < 0 || Integer.parseInt(ip) > 255) {
                            return false;
                        }
                    }
                } catch (Exception e) {
                    return false;
                }
                return true;
            } else {
                return false;
            }
        }
        return ipAddress;
    }

    public static String getFirstInstallTime() {
        PackageManager packageManager = ApplicationShare.getApp().getApplicationContext().getPackageManager();
        long firstInstallTime = 0;
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(ApplicationShare.getApp().getApplicationContext().getPackageName(), 0);
            firstInstallTime = packageInfo.firstInstallTime; // 应用第一次安装的时间

        } catch (Exception e) {
            e.printStackTrace();
            return firstInstallTime + "";
        }
        return firstInstallTime + "";
    }
}
